package plug.creatures;

import java.awt.event.ActionListener;
import java.lang.reflect.Constructor;
import java.util.Map;
import java.util.logging.Logger;

import javax.swing.ButtonGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;

import creatures.ICreature;

public class PluginMenuItemBuilder {

  private JMenu menu;

  private Map<String,Constructor<? extends ICreature>> constructors;
  
  private Map<String,Integer> statistics;

  private ActionListener listener;
  
  private static Logger logger = Logger.getLogger("plug.Menu");

  public PluginMenuItemBuilder(Map<String,Constructor<? extends ICreature>> mc,
                               ActionListener listener) {
    menu = new JMenu();
    this.constructors = mc;
    this.listener = listener;
  }

  public PluginMenuItemBuilder(ActionListener listener, Map<String, Integer> statMap) {
	menu = new JMenu();
	this.statistics = statMap;
	this.listener = listener;
  }

  public void setMenuTitle(String title) {
    menu.setText(title);
  }

  public void buildMenuWithCheckBox() {
    logger.info("Building plugin menu");
    menu.removeAll();
    for (String name : constructors.keySet()) {
      // ActionCommand contains the name of the plugin = key in the map
    	JCheckBoxMenuItem mi = new JCheckBoxMenuItem(name);
    	mi.setActionCommand(name);
      mi.addActionListener(listener);
      menu.add(mi);
    }
  }
  
  public void builMenuWithCheckBoxGroup() {
	  logger.info("Building plugin menu");
	    menu.removeAll();
	    ButtonGroup group = new ButtonGroup();
	    for (String name : constructors.keySet()) {
	      // ActionCommand contains the name of the plugin = key in the map
	    	JCheckBoxMenuItem mi = new JCheckBoxMenuItem(name);
	    	mi.setActionCommand(name);
	      mi.addActionListener(listener);
	      menu.add(mi);
	      group.add(mi);
	    }
  }
  
  public void buildStatMenuWithCheckBox() {
	    logger.info("Building plugin menu");
	    menu.removeAll();
	    ButtonGroup group = new ButtonGroup();
	    for (String name : this.statistics.keySet()) {
	      // ActionCommand contains the name of the plugin = key in the map
	    	JCheckBoxMenuItem mi = new JCheckBoxMenuItem(name);
	    	mi.setActionCommand(name);
	      mi.addActionListener(listener);
	      menu.add(mi);
	      group.add(mi);
	      }
  }

  public JMenu getMenu() {
    return menu;
  }

}
